package com.ruoyi.app.controller;

import com.github.pagehelper.PageHelper;
import com.ruoyi.app.service.IDistributorService;
import com.ruoyi.app.vo.DistributorInfoVo;
import com.ruoyi.business.constant.JtUserConstants;
import com.ruoyi.business.domain.JtAppUser;
import com.ruoyi.business.domain.JtCommissionDetail;
import com.ruoyi.business.domain.JtCountryCode;
import com.ruoyi.business.domain.JtDistributor;
import com.ruoyi.business.service.IJtCommissionDetailService;
import com.ruoyi.business.service.IJtCountryCodeService;
import com.ruoyi.business.service.IJtDistributorService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.JtImageUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 分销商
 * @Author Alan Zhang
 * @Date 2025/3/15 15:17
 */
@RestController
@RequestMapping("/api/distributor")
public class DistributorController extends BaseApiController{
    @Autowired
    private IJtDistributorService jtDistributorService;
    @Autowired
    private IDistributorService distributorService;
    @Autowired
    private IJtCommissionDetailService commissionDetailService;
    @Autowired
    private IJtCountryCodeService jtCountryCodeService;

    /**
     *  申请
     * @param distributorInfoVo  分销商
     * @return
     */
    @PostMapping("/apply")
    public AjaxResult apply(DistributorInfoVo distributorInfoVo,
                            @RequestParam(value = "file1", required = false) MultipartFile file1,
                            @RequestParam(value = "file2", required = false)  MultipartFile file2) throws IOException {
        JtAppUser jtAppUser = getTokenAppUser();

        JtDistributor param = new JtDistributor();
        param.setUserId(jtAppUser.getUserId());
        List<JtDistributor> jtDistributorList = jtDistributorService.selectJtDistributorList(param);

        if(jtDistributorList.size()> 0){
            return AjaxResult.error("已申请，请勿重复申请！");
        }

        StringBuilder msgSb = new StringBuilder();
        if(distributorInfoVo.getCountryCode() == null || StringUtils.isEmpty(distributorInfoVo.getPhoneNumber())){
            msgSb.append("国家码和手机号不能为空！");
        }
        if(distributorInfoVo.getType()==null){
            msgSb.append("类型不能为空！");
        }
        if(JtUserConstants.COMPANY_DISTRIBUTOR_TYPE == distributorInfoVo.getType()){
            if(StringUtils.isEmpty(distributorInfoVo.getLicenseName())){
                msgSb.append("请填写公司名称！");
            }
            if(StringUtils.isEmpty(distributorInfoVo.getLicenseNo())){
                msgSb.append("请填写营业执照/证件号！");
            }
            if(StringUtils.isEmpty(distributorInfoVo.getCorporation())){
                msgSb.append("请填写公司法人！");
            }
            if(file1 == null){
                msgSb.append("请上传营执照/证件图片！");
            }
        } else if(JtUserConstants.PERSON_DISTRIBUTOR_TYPE == distributorInfoVo.getType()){
            if(StringUtils.isEmpty(distributorInfoVo.getLicenseName())){
                msgSb.append("姓名不能为空！");
            }
            if(StringUtils.isEmpty(distributorInfoVo.getLicenseNo())){
                msgSb.append("营业执照/证件号不能为空！");
            }
            if(file1 == null){
                msgSb.append("请上传身份证件正面图片！");
            }
            if(file2 == null){
                msgSb.append("请上传身份证件反面图片！");
            }
        } else {
            msgSb.append("类型错误！");
        }

        if(StringUtils.isEmpty(distributorInfoVo.getDescription())){
            msgSb.append("请说明情况！");
        }

        if(msgSb.length() > 0){
            return AjaxResult.error(msgSb.toString());
        }
        JtCountryCode countryCodeParam = new JtCountryCode();
        countryCodeParam.setCountryCode(distributorInfoVo.getCountryCode());
        List<JtCountryCode> countryCodeList = jtCountryCodeService.selectJtCountryCodeList(countryCodeParam);

        JtDistributor jtDistributor = new JtDistributor();
        jtDistributor.setUserId(jtAppUser.getUserId());
        if(countryCodeList.size() > 0) {
            jtDistributor.setCurrency(countryCodeList.get(0).getCurrency());
        }
        jtDistributor.setDistributorName(distributorInfoVo.getName());
        jtDistributor.setLicenseName(distributorInfoVo.getLicenseName());
        jtDistributor.setLicenseNo(distributorInfoVo.getLicenseNo());
        if (file1 != null) {
            jtDistributor.setLicenseImage1Url(JtImageUtil.uploadDistributor(file1, true));
        }
        if (file2 != null) {
            jtDistributor.setLicenseImage2Url(JtImageUtil.uploadDistributor(file2, true));
        }
        jtDistributor.setCorporation(distributorInfoVo.getCorporation());
        jtDistributor.setDescription(distributorInfoVo.getDescription());
        jtDistributor.setCountryCode(distributorInfoVo.getCountryCode());
        jtDistributor.setPhoneNumber(distributorInfoVo.getPhoneNumber());
        jtDistributor.setEmail(distributorInfoVo.getEmail());
        jtDistributor.setContact(distributorInfoVo.getContacts());
        jtDistributor.setType(distributorInfoVo.getType());
        return distributorService.applyDistributor(jtDistributor);
    }

    /**
     *  申请信息
     * @return
     */
    @PostMapping("/apply/info")
    public AjaxResult applyInfo()
    {
        JtAppUser jtAppUser = getTokenAppUser();

        JtDistributor param = new JtDistributor();
        param.setUserId(jtAppUser.getUserId());
        List<JtDistributor> jtDistributorList = jtDistributorService.selectJtDistributorList(param);

        return AjaxResult.success("success", jtDistributorList.get(0));
    }


    /**
     *  佣金明细
     * @param type 收入  提现
     * @return
     */
    @PostMapping("/commission/list")
    public AjaxResult commissionList(int type, Date startTime, Date endTime, int pageNum, int pageSize)
    {
        JtAppUser jtAppUser = getTokenAppUser();

        JtCommissionDetail param = new JtCommissionDetail();
        param.setUserId(jtAppUser.getUserId());
        param.getParams().put("startTime", startTime);
        param.getParams().put("endTime", endTime);

        PageUtils.startPage();
        //排序字段
        PageHelper.orderBy("create_time desc");
        List<JtCommissionDetail> commissionDetailList = commissionDetailService.selectJtCommissionDetailList(param);
        return AjaxResult.success("success", commissionDetailList);
    }
}
